package com.colorlight.cloud.complete.entity.enumerate;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.gateway.api.message.constraint.basics.ienum.EnumTrans;
import com.colorlight.iot.proto.complete.entity.CompleteEntity;
import lombok.Getter;

import java.util.Arrays;
import java.util.Objects;

@Getter
public enum DownloadResult implements EnumTrans<CompleteEntity.DownloadResult, DownloadResult> {
    /**
     * 默认属性
     * 成功下载
     * 下载失败-没有足够的空间下载文件
     * 下载失败-访问URL失败
     * 下载失败-MD5校验失败
     * 下载失败-长度校验失败
     * 正在下载
     */
    DownloadResultDefault(CompleteEntity.DownloadResult.DownloadResultDefault),
    successDownload(CompleteEntity.DownloadResult.successDownload),
    errorDownloadNoSpace(CompleteEntity.DownloadResult.errorDownloadNoSpace),
    errorDownloadFailedToAccessURL(CompleteEntity.DownloadResult.errorDownloadFailedToAccessURL),
    errorDownloadMD5Fail(CompleteEntity.DownloadResult.errorDownloadMD5Fail),
    errorDownloadLengthFail(CompleteEntity.DownloadResult.errorDownloadLengthFail),
    downloading(CompleteEntity.DownloadResult.downloading),
    ;

    private final Integer id;
    private final String name;
    private final CompleteEntity.DownloadResult downloadResult;

    DownloadResult(CompleteEntity.DownloadResult downloadResult) {
        this.id = downloadResult.getNumber();
        this.name = downloadResult.name();
        this.downloadResult = downloadResult;
    }

    /**
     * @param downloadResult
     * @return 对应的类型
     */
    public static DownloadResult getDownloadResultStruct(CompleteEntity.DownloadResult downloadResult) {
        Objects.requireNonNull(downloadResult, "downloadResult can not be null.");
        return Arrays.stream(DownloadResult.values())
                .filter(e -> e.name.equals(downloadResult.name()))
                .findFirst()
                .orElseThrow(() -> new ParameterException(ExceptionEnum.NOT_EXIST_ENUM));
    }

    @Override
    public CompleteEntity.DownloadResult toProto() {
        return this.downloadResult;
    }

    @Override
    public DownloadResult formProto(CompleteEntity.DownloadResult downloadResult) {
        return getDownloadResultStruct(downloadResult);
    }
}

